package web.servlets;

import domain.Account;
import service.AccountService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class SaveInformationServlet extends HttpServlet {

    private String username = null;
    private String password = null;
    private String repeatedPassword = null;
    private String email = null;
    private String lastname = null;
    private String firstname = null;
    private String status = null;
    private String address1 = null;
    private String address2 = null;
    private String city = null;
    private String state = null;
    private String zip = null;
    private String country = null;
    private String phone = null;
    private String languPr = null;
    private  String favoricate = null;

    private String VIEW_SIGNONFORM="/WEB-INF/jsp/account/SignonForm.jsp";
    private static final String ERROR_MESSAGE="/WEB-INF/jsp/common/Error.jsp";
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.setCharacterEncoding("UTF-8");
        username = req.getParameter("username");
        AccountService accountService1 = new AccountService();

        Account account1 = accountService1.getAccount(username);

        password = req.getParameter("password");
        repeatedPassword= req.getParameter("repeatedPassword");
        email = req.getParameter("email");
        lastname = req.getParameter("lastname");
        firstname = req.getParameter("firstname");
        status = req.getParameter("status");
        address1 = req.getParameter("address1");
        address2 = req.getParameter("address2");
        city = req.getParameter("city");
        state = req.getParameter("state");
        zip = req.getParameter("zip");
        country = req.getParameter("country");
        phone = req.getParameter("phone");
        languPr = req.getParameter("languagePreference");
        favoricate = req.getParameter("favouriteCategoryId");
        String listOption = req.getParameter("listOption");
        String bannerOption = req.getParameter("bannerOption");
        if(account1==null) {
            if (!"".equals(username) && !"".equals(password) && !"".equals(repeatedPassword) && !"".equals(email) && !"".equals(lastname) && !"".equals(firstname) && !"".equals(status) &&
                    !"".equals(address1) && !"".equals(address2) && !"".equals(city) && !"".equals(state) && !"".equals(zip) &&
                    !"".equals(country) && !"".equals(phone) && !"".equals(languPr)) {
                if (password.equals(repeatedPassword)) {
                    AccountService accountService = new AccountService();
                    Account account = new Account();
                    account.setUsername(username);
                    account.setPassword(password);
                    account.setPhone(phone);
                    account.setCountry(country);
                    account.setState(state);
                    account.setCity(city);
                    account.setAddress1(address1);
                    account.setAddress2(address2);
                    account.setStatus(status);
                    account.setFirstName(firstname);
                    account.setLastName(lastname);
                    account.setLanguagePreference(languPr);
                    account.setZip(zip);
                    account.setFavouriteCategoryId(favoricate);
                    account.setEmail(email);
                    account.setListOption(Boolean.parseBoolean(listOption));
                    account.setBannerOption(Boolean.parseBoolean(bannerOption));
                    accountService.insertAccount(account);

                    req.getRequestDispatcher(VIEW_SIGNONFORM).forward(req, resp);
                } else {
                    HttpSession session = req.getSession();
                    session.setAttribute("message", "Failed! The entered passwords are inconsistent,Ensure that the passwords entered are the same!");
                    req.getRequestDispatcher(ERROR_MESSAGE).forward(req, resp);
                }
            } else {
                HttpSession session = req.getSession();
                session.setAttribute("message", "Failed! The information form must not be blank!");
                req.getRequestDispatcher(ERROR_MESSAGE).forward(req, resp);
            }
        }else{
            HttpSession session = req.getSession();
            session.setAttribute("message", "Failed! The user name already exists. Please enter it again!");
            req.getRequestDispatcher(ERROR_MESSAGE).forward(req, resp);
        }

    }
}
